package org.aceor.mddal.config.sharding;

import com.google.common.base.Objects;
import com.google.common.base.Strings;
import org.aceor.mddal.common.sharding.ShardingFunction;

import java.text.MessageFormat;

/**
 * Created by lxue on 16/3/26.
 */
public class TableRule {
    private String dbNamePattern;
    private String tbNamePattern;
    private ShardingFunction tbRule;
    private ShardingFunction dbRule;

    private MessageFormat dbNameMessageFormat;
    private MessageFormat tbNameMessageFormat;

    public void init() {
        if (!Strings.isNullOrEmpty(dbNamePattern)) {
            dbNameMessageFormat = new MessageFormat(dbNamePattern);
        }
        if (!Strings.isNullOrEmpty(tbNamePattern)) {
            tbNameMessageFormat = new MessageFormat(tbNamePattern);
        }
    }

    public String getDbNamePattern() {
        return dbNamePattern;
    }

    public void setDbNamePattern(String dbNamePattern) {
        this.dbNamePattern = dbNamePattern;
    }

    public String getTbNamePattern() {
        return tbNamePattern;
    }

    public void setTbNamePattern(String tbNamePattern) {
        this.tbNamePattern = tbNamePattern;
    }

    public ShardingFunction getTbRule() {
        return tbRule;
    }

    public void setTbRule(ShardingFunction tbRule) {
        this.tbRule = tbRule;
    }

    public ShardingFunction getDbRule() {
        return dbRule;
    }

    public void setDbRule(ShardingFunction dbRule) {
        this.dbRule = dbRule;
    }

    public MessageFormat getDbNameMessageFormat() {
        return dbNameMessageFormat;
    }

    public MessageFormat getTbNameMessageFormat() {
        return tbNameMessageFormat;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this)
                .add("dbNamePattern", dbNamePattern)
                .add("tbNamePattern", tbNamePattern)
                .add("tbRule", tbRule)
                .add("dbRule", dbRule)
                .toString();
    }
}
